package dao

import (
	"context"
	"mall-portalv2/internal/model"
)

func (d *Dao) CreatePayLogs(ctx context.Context, insertData model.OmsPayLogs) (model.OmsPayLogs, error) {
	tb := model.OmsPayLogs{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Create(&insertData).Error; err != nil {
		return tb, err
	}
	return insertData, nil
}

func (d *Dao) QueryPayLogsWhereOrderIdAndStatus(ctx context.Context, OrderId int64, payType int, CloseStatus int) (model.OmsPayLogs, error) {
	tb := model.OmsPayLogs{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("order_id = ? AND pay_type = ? AND close_status = ?", OrderId, payType, CloseStatus).Order("id DESC").First(&tb).Error; err != nil {
		return tb, err
	}
	return tb, nil
}

func (d *Dao) UpdatePayLogsWhereOrderId(ctx context.Context, OrderId int64, upadtes map[string]interface{}) error {
	tb := model.OmsPayLogs{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("order_id = ?", OrderId).Updates(upadtes).Error; err != nil {
		return err
	}
	return nil
}

func (d *Dao) QueryPayLogsWhereOrderIdNoWay(ctx context.Context, OrderId int64) ([]model.OmsPayLogs, error) {
	tb := model.OmsPayLogs{}
	result := []model.OmsPayLogs{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("order_id = ?", OrderId).Order("id DESC").Find(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryPayLogsWherePaySnNoWay(ctx context.Context, PaySn string) (model.OmsPayLogs, error) {
	tb := model.OmsPayLogs{}
	result := model.OmsPayLogs{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("pay_sn = ?", PaySn).Order("id DESC").First(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryPayLogsWhereOrderIdAndPaySnNoWay(ctx context.Context, OrderId int64, PaySn string) (model.OmsPayLogs, error) {
	tb := model.OmsPayLogs{}
	result := model.OmsPayLogs{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("pay_sn = ? AND order_id = ?", PaySn, OrderId).Order("id DESC").First(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}
